﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ITAP.Core.Modelling.Primitives;
using System.Drawing;
using ITAP.Core.Calculations;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Components.Dimensions
{
    public enum DiameterDimensionType
    {
        Left,
        Right
    }

    [Serializable]
    public class DiameterDimension : Dimension
    {
        private int offsetFromHoleInPixels = 10;

        public int OffsetFromHoleInPixels
        {
            get { return offsetFromHoleInPixels; }
            set { offsetFromHoleInPixels = value; }
        }

        private ModelVector vector = new ModelVector(-1, 1);

        public override ModelPoint Position
        {
            get
            {
                float modelOffsetFromHole = coordinateConverter.ScreenMeasureToModelMeasure(offsetFromHoleInPixels, screenWidth, screenHeight, modelRectangle);
                vector.Length = modelOffsetFromHole;
                RectangleF bbox = this.Hole.BoundingBox;
                if (this.type == DiameterDimensionType.Left)
                {

                    ModelPoint edgePoint = new ModelPoint(bbox.X, bbox.Y + bbox.Height);
                    return ModelCalculations.GetOffsetPoint(edgePoint, vector);
                }
                else
                {
                    ModelPoint edgePoint = new ModelPoint(bbox.X + bbox.Width, bbox.Y + bbox.Height);
                    return ModelCalculations.GetOffsetPoint(edgePoint, vector);
                }
            }
        }

        public ModelPoint StartDiameterLinePoint
        {
            get
            {
                float modelOffsetFromHole = coordinateConverter.ScreenMeasureToModelMeasure(offsetFromHoleInPixels, screenWidth, screenHeight, modelRectangle);
                vector.Length = modelOffsetFromHole;
                RectangleF bbox = this.Hole.BoundingBox;
                if (this.type == DiameterDimensionType.Left)
                    return new ModelPoint(bbox.X + bbox.Width, bbox.Y);
                else
                    return new ModelPoint(bbox.X, bbox.Y);
            }
        }

        public ModelVector Vector
        {
            get { return vector; }
        }

        private DiameterDimensionType type = DiameterDimensionType.Left;

        public DiameterDimensionType Type
        {
            get { return type; }
            set { type = value; }
        }

        public ContourHole Hole
        {
            get
            {
                foreach (IContourComponent component in this.connectedComponents)
                    if (component is ContourHole)
                        return component as ContourHole;

                return null;
            }
        }
        public override string DimensionText
        {
            get
            {
                return "Ø" + (this.Hole.Radius * 2).ToString("0.0");
            }
        }

        public override void Recalculate()
        {
            if (this.type == DiameterDimensionType.Left)
            {
                vector.X = -1;
                vector.Y = 1;
            }
            else
            {
                vector.X = 1;
                vector.Y = 1;
            }
        }

        public override void AcceptDimensionVisitor(IDimensionVisitor visitor)
        {
            visitor.Visit(this);
        }

        public override bool IsPrimitiveDependsOnDisplayParameters
        {
            get
            {
                return true;
            }
        }

        public override RectangleF BoundingBox
        {
            get
            {
                return this.Hole.BoundingBox;
            }
        }

        public override ModelVector GetSizeLineNormal()
        {
            return new ModelVector(0, -1);
        }

        public override void MoveTo(ModelPoint destination)
        {
            
        }

        public override void MoveWithOffset(ModelVector offset)
        {
            
        }
    }
}
